package com.bonc.modules.rpt.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.bonc.common.utils.Result;
import com.bonc.modules.common.entity.StdCode;
import com.bonc.modules.common.service.CommonService;
import com.bonc.modules.common.service.StdCodeService;
import com.bonc.modules.rpt.param.SurveyParam;
import com.bonc.modules.rpt.result.SurveyResult;
import com.bonc.modules.rpt.service.ServSatisSurveyItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

@RestController
@RequestMapping("/rpt/servSatisSurveyItem")
public class ServSatisSurveyItemController {

    @Autowired
    private ServSatisSurveyItemService servSatisSurveyItemService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private StdCodeService stdCodeService;

    private static char[] cnArr = new char[]{'一', '二', '三', '四', '五', '六', '七', '八', '九'};

    /**
     * 获取图数据
     *
     * @return
     */
    @PostMapping("/getChartData/{chartType}")
    public Result getChartData(@RequestBody SurveyParam surveyParam, @PathVariable String chartType) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Map<String, StdCode> chartTypeMap = stdCodeService.getConfMap("SATIS_CHART_TYPE");
        StdCode stdCode = MapUtil.get(chartTypeMap, chartType, StdCode.class);

        try {
            Integer index = stdCode.getStdConf().lastIndexOf(".");
            String className = stdCode.getStdConf().substring(0, index);
            String methodName = stdCode.getStdConf().substring(index + 1);

            Class clazz = Class.forName(className);
            Method getChartData = clazz.getMethod(methodName, SurveyParam.class);
            SurveyResult servSatisSurveyItemResult = (SurveyResult) getChartData.invoke(SpringUtil.getBean(clazz), surveyParam);

            return Result.ok().put("data", servSatisSurveyItemResult);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return Result.error();
    }
}
